<?PHP
/*
  Library of commonly used functions
  $Id: functions.inc,v 1.4 2001/09/26 20:01:23 eugenem Exp eugenem $
  $Header: /export/home/eugenem/public_html/ComplianceReports/RCS/functions.inc,v 1.4 2001/09/26 20:01:23 eugenem Exp eugenem $
  $Log: functions.inc,v $
  Revision 1.4  2001/09/26 20:01:23  eugenem
  Changed listbox*_field functions to include a default value of 0 in the list of choices.

  Revision 1.3  2001/08/29 20:23:37  eugenem
  Added function to calc coefficient of variation

  Revision 1.2  2001/08/22 19:14:22  eugenem
  Added a one-field version of listbox_field for cases where only one column is selected by a query.

  Revision 1.1  2001/08/14 18:43:25  eugenem
  Initial revision

*/

/* Generate a SELECT form element from results of a DB query */
function listbox_field ($mysql_link, $name, $default=0) {
/*
$mysql_link - MySQL link identifier returned by mysql_connect()
$name - name to be assigned to the SELECT box
$default - item in the SELECT list that will be selected by default
Returns a text string containing the HTML code for the SELECT box
*/

  $result="<select name=\"$name\" size=1>\n"; 
  $result .= "<option value=\"0\">Select $name</option>\n";
  while (list($key,$val)=mysql_fetch_array($mysql_link)) { 
    if ($default  == $key) {
      $selected="selected";
    } else {
      $selected="";
    } 
    $result.="<option value=\"$key\" $selected>$val</option>\n"; 
  } 
  $result.="</select>\n"; 
  return $result;
}

/*
   Generate a SELECT form element from results of a DB query when only
   one column is returned from the query
*/
function listbox_one_field ($mysql_link, $name, $default=0) {
/*
$mysql_link - MySQL link identifier returned by mysql_connect()
$name - name to be assigned to the SELECT box
$default - item in the SELECT list that will be selected by default
Returns a text string containing the HTML code for the SELECT box
*/

  $result = "<select name=\"$name\" size=1>\n";
  $result .= "<option value=\"0\">Select $name</option>\n";
  while (list($val) = mysql_fetch_array($mysql_link)) {
    if ($default == $val) {
      $selected = "selected";
    } else {
      $selected = "";
    }
    $result .= "<option value=\"$val\" $selected>$val</option>\n";
  }
  $result .= "</select>\n";
  return $result;
}

/*
  Calculate the coefficient of variation (Std dev/Mean) as a percentage
*/
function cv($mean,$stdev) {
/*
$mean - mean value of a group of numbers
$stdev - standard deviation of a group of numbers
Returns the coefficient of variation defined as $stdev/$mean as a percentage
*/

  $cv = ($stdev/$mean)*100;
  return $cv;
}

/*
  Strip HTML and PHP tags from HTTP_GET/POST/COOKIE_VARS

  Nabbed from the annotated PHP manual at http://www.php.net/
  Written by
  tomjr@tx3.net
  jkeppens@xs4all.be
  dsalada@adventassociates.com
*/
function strip_http_vars($vars) {
  if(is_array($vars)) {
    reset($vars); 
    while (list($key, $val) = each($vars)) { 
      if (is_array($val)) { 
        while (list($akey,$aval) = each($val)) { 
          $vars[$key][$akey] = strip_tags($aval); 
          $$key[$akey] = strip_tags($aval); 
        } 
      } else { 
        $vars[$key] = strip_tags($val); 
        $$key = strip_tags($val); 
      } 
    }
  }
  reset($vars);
  return $vars; 
}

function cleanup($textinput,$cleanup="0",$wrapspace_after="5000") { 
/*
  Uli Pahlke " www.screengraphics.de " ========================= 
  function to cleanup textinputs & textareas 
  wraps strings after a specified stringlength if ask for 

  call function = cleanup($textinput,0,5000);

  $textinput == StringSource / z.B.: textfelder, textareas usw. 
  $cleanup == 0 only digits & letters allowed (default = 0) 
  $cleanup == 1 only letters allowed 
  $cleanup == 2 only digits allowed 
  $cleanup == 3 no spaces 
  $wrapspace_after == wraps strings after a specified stringlength if ask for (default = 5000) 
  bei $cleanup == 3 / no wrapping 
*/
  
  switch($cleanup) {
    case 0:
      $clean = eregi_replace ("[[:punct:]]","",$textinput); 
      $wrapped = wordwrap( $clean,$wrapspace_after,"\n",1); 
      break;
    case 1:
      $clean = eregi_replace ("[[:digit:][:punct:]]","",$textinput); 
      $wrapped = wordwrap( $clean,$wrapspace_after,"\n",1);  
      break;
    case 2:
      $clean = eregi_replace ("[[:alpha:][:punct:]]","",$textinput); 
      $wrapped = wordwrap( $clean,$wrapspace_after,"\n",1); 
      break;
    case 3:
      $clean = trim(eregi_replace("[[:space:]]", "" , $textinput)); 
      break;
  }
  if($cleanup == 3) { 
    return stripslashes($clean); 
  } else { 
    $clean = $wrapped; 
    return stripslashes($clean); 
  } 
} 

function validate_form_input($value) {

}

?>
